package com.ajk.server.service.impl;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.dao.DiseaseKeywordMapper;
import com.ajk.server.model.DiseaseKeyword;
import com.ajk.server.model.query.DiseaseKeywordQuery;
import com.ajk.server.service.DiseaseKeywordService;
import com.ajk.server.utils.RedisKeyUtil;

/**
 * 疾病搜索热词表  <br>
 * t_disease_keyword <br>
 * v1.0 2016-05-01
 */
 @Service
 public class DiseaseKeywordServiceImpl implements DiseaseKeywordService {
	
	private static final Logger logger = LoggerFactory.getLogger(DiseaseKeywordServiceImpl.class);
	
	@Autowired
	private DiseaseKeywordMapper diseaseKeywordMapper;
	
	@Autowired
	RedisTemplate<String, Object> template;
	
	/**
 	*根据ID删除 疾病搜索热词表
 	*/
 	@Override
 	public Integer deleteDiseaseKeywordById(Integer id){
 		return diseaseKeywordMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 疾病搜索热词表
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertDiseaseKeyword(List<? extends DiseaseKeyword> diseaseKeywords){
		Integer count = diseaseKeywordMapper.batchInsert(diseaseKeywords);
		return count;
 	}
 	/**
 	*新增 疾病搜索热词表
 	*/
 	@Override
 	@Transactional
 	public Integer insertDiseaseKeyword(DiseaseKeyword domain){
		diseaseKeywordMapper.insert(domain);
		Integer id = domain.getId();
		syncSingletonRedis(id);
		return id;
 	}

 	/**
 	*更新 疾病搜索热词表
 	*/
 	@Override
 	@Transactional
 	public Integer updateDiseaseKeyword(DiseaseKeyword domain){
		diseaseKeywordMapper.update(domain);
		Integer id = domain.getId();
		syncSingletonRedis(id);
		return id;
 	}
 	
 	/**
 	*根据ID查询 疾病搜索热词表
 	*/
 	public DiseaseKeyword findDiseaseKeywordById(Integer id){
 		DiseaseKeyword result = diseaseKeywordMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 疾病搜索热词表
 	*/
	public List<DiseaseKeyword> findDiseaseKeywordByQuery(DiseaseKeywordQuery query){
		query.dealWith();
		List<DiseaseKeyword> result = diseaseKeywordMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			for (DiseaseKeyword diseaseKeyword : result) {
				diseaseKeyword.dealWith();
			}
		}
		return result;
	}	
	
	@Override
	public List<DiseaseKeyword> findDiseaseKeywordByQueryWithCache() {
		String key = RedisKeyUtil.keywordHashKey();
		if(!template.hasKey(key)){
			syncInRedis(true);
		}
		HashOperations<String, String, DiseaseKeyword> operations = template.opsForHash();

		List<DiseaseKeyword> dbList = operations.values(key);
		Collections.sort(dbList);
		return dbList;
	}
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countDiseaseKeywordByQuery(DiseaseKeywordQuery query){
 		query.dealWith();
 		Integer result = diseaseKeywordMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<DiseaseKeyword> findDiseaseKeywordByIds(List<Integer> ids){
 		List<DiseaseKeyword> result = diseaseKeywordMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (DiseaseKeyword diseaseKeyword : result) {
				diseaseKeyword.dealWith();
			}
		}
		return result;
 	}

	@Override
	public void syncSingletonRedis(Integer keywordId) {
		String key = RedisKeyUtil.keywordHashKey();
		
		if(!template.hasKey(key)){
			syncInRedis(true);
		}else{
			DiseaseKeyword keyword = diseaseKeywordMapper.findById(keywordId);
			String hashKey = RedisKeyUtil.domainIdKey(keywordId);
			if(keyword==null || keyword.getInUsed()==0){
				template.opsForHash().delete(key, hashKey);
			}else{
				template.opsForHash().put(key, hashKey, keyword);
			}
		}
		
	}

	@Override
	public void syncInRedis(boolean force) {
		String key = RedisKeyUtil.keywordHashKey();
		if(force || (!template.hasKey(key) )){
			template.delete(key);
			List<DiseaseKeyword> kDiseaseKeywords = diseaseKeywordMapper.findAll();
			Map<String, DiseaseKeyword> map = new HashMap<String, DiseaseKeyword>();
			if (CollectionUtils.isNotEmpty(kDiseaseKeywords)) {
				for (DiseaseKeyword diseaseKeyword : kDiseaseKeywords) {
					String hashKey = RedisKeyUtil.domainIdKey(diseaseKeyword.getId());
					map.put(hashKey, diseaseKeyword);
				}
				if(!map.isEmpty()){
					template.opsForHash().putAll(key, map);
				}
			}
		}
		
	}
}	